package com.ruoyi.common.utils;

import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

/**
 * Created On : 2024/4/1.
 * Author : xuliang
 * Description: LastRowBackgroundHandler
 * <p>
 */
@Slf4j
public class LastRowBackgroundHandler implements RowWriteHandler {

    private IndexedColors backgroundColor;

    private int lastIndex;

    public LastRowBackgroundHandler(IndexedColors backgroundColor,int lastIndex) {
        this.backgroundColor = backgroundColor;
        this.lastIndex = lastIndex;
    }

    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        // Do nothing
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        Sheet sheet = writeSheetHolder.getSheet();
        int lastRowNum = sheet.getLastRowNum();
        log.info("lastRowNum：" + lastRowNum);
        if (relativeRowIndex.equals(lastIndex) && !isHead) {
            log.info("relativeRowIndex：" + relativeRowIndex);
            if(row.getCell(0) != null){
                if(row.getCell(0).getCellStyle() != null){
                    CellStyle originalStyle = row.getCell(0).getCellStyle();

                    for (int i = 0; i < row.getLastCellNum(); i++) {
                        Cell cell = row.getCell(i);
                        if (cell != null) {
                            CellStyle newStyle = createNewCellStyle(sheet.getWorkbook(), originalStyle);
                            if (i == 7) { // 检查是否为第八个单元格
                                applyPercentageFormat(newStyle, sheet.getWorkbook());
                            }
                            applyBackgroundColor(newStyle);
                            applyBorderStyle(newStyle, originalStyle);
                            cell.setCellStyle(newStyle);
                        }
                    }
                }
            }
        }
    }

    private CellStyle createNewCellStyle(Workbook workbook, CellStyle originalStyle) {
        CellStyle newStyle = workbook.createCellStyle();
        newStyle.cloneStyleFrom(originalStyle);
        return newStyle;
    }

    private void applyBackgroundColor(CellStyle style) {
        style.setFillForegroundColor(backgroundColor.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }

    private void applyBorderStyle(CellStyle newStyle, CellStyle originalStyle) {
        newStyle.setBorderTop(originalStyle.getBorderTop());
        newStyle.setBorderRight(originalStyle.getBorderRight());
        newStyle.setBorderBottom(originalStyle.getBorderBottom());
        newStyle.setBorderLeft(originalStyle.getBorderLeft());
    }

    private void applyPercentageFormat(CellStyle style, Workbook workbook) {
        DataFormat format = workbook.createDataFormat();
        style.setDataFormat(format.getFormat("0.00%"));
    }

}



